import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fwanandroid/model/api/WanAndroidService.dart';
import 'package:fwanandroid/ui/blog/BlogPage.dart';
import 'package:fwanandroid/ui/home/HomePage.dart';
import 'package:fwanandroid/ui/home/NavigationPage.dart';
import 'package:fwanandroid/ui/home/ProjectPage.dart';
import 'package:fwanandroid/ui/home/SquarePage.dart';
import 'package:fwanandroid/ui/home/SystemPage.dart';

import '../MyColors.dart';

class BlogTabPage extends StatefulWidget {
  const BlogTabPage({super.key});

  @override
  State<BlogTabPage> createState() => _BlogTabPageState();
}

class _BlogTabPageState extends State<BlogTabPage> {
  var _tabs = <Tab>[];
  var _tabBarViews = <Widget>[];

  @override
  void initState() {
    super.initState();
    loadBlogType();
  }

  @override
  Widget build(BuildContext context) {
    if(_tabs.isEmpty){
      return Container();
    }else {
      return  DefaultTabController(
          length: _tabs.length,
          child: SafeArea(
            child: Scaffold(
              appBar: PreferredSize(
                preferredSize: Size.fromHeight(56),
                child: Material(
                  color: Colors.white,
                  child: TabBar(
                    isScrollable:true,
                    labelColor: MyColors.leiMuBlue,
                    indicatorColor: MyColors.leiMuBlue,
                    tabs: _tabs,
                    tabAlignment: TabAlignment.start,
                  ),
                ),
              ),
              body:  TabBarView(
                children: _tabBarViews,
              ),
            ),
          ));
    }
  }

  loadBlogType() {
    EasyLoading.show(status: 'loading...');
    WanAndroidService.getBlogType().then((value) {
      setState(() {
        _tabs = value.map((item) {
          return Tab(
            text: "${item.name}",
          );
        }).toList();
        _tabBarViews = value.map((item) {
          return BlogPage(cid: item.id);
        }).toList();
      });
    }).whenComplete((){
      EasyLoading.dismiss();
    });
  }
}
